package org.example.studyonline.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.studyonline.mapper.UserMapper;
import org.example.studyonline.mapper.UserRoleMapper;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.VO.CourseListVO;
import org.example.studyonline.pojo.VO.UserInfoVO;
import org.example.studyonline.pojo.User;
import org.example.studyonline.pojo.UserRole;
import org.example.studyonline.pojo.DTO.RegisterDTO;
import org.example.studyonline.pojo.VO.UserListVo;
import org.example.studyonline.service.UserService;
import org.example.studyonline.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.baomidou.mybatisplus.extension.toolkit.Db.page;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String register(RegisterDTO registerDTO) {
        // 检查邮箱是否已注册
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", registerDTO.getEmail());
        User user = userMapper.selectOne(queryWrapper);  // 查找是否已注册
        if (user == null) {
            // 创建新用户
            User newUser = new User();
            newUser.setName(registerDTO.getName());
            newUser.setEmail(registerDTO.getEmail());
            newUser.setPassword(registerDTO.getPassword());
            newUser.setRegistrationDate(new Date());

            int rowsAffected = userMapper.insert(newUser);

            if (rowsAffected > 0) {
                // 成功插入用户后，直接通过 newUser 获取 userID
                Integer userID = newUser.getUserID();
                UserRole userRole = new UserRole();
                userRole.setUserID(userID);
                userRole.setRoleID(1); // 设置为普通用户角色

                // 插入用户角色
                userRoleMapper.insert(userRole);

                return null;
            } else {
                return "注册失败，请稍后重试";
            }

        } else {
            return "邮箱已注册";
        }

    }

    @Override
    public UserInfoVO getUserInfo(Integer userID) {
        UserInfoVO u = userMapper.getUserInfo(userID);
        return u;
    }

    @Override
    public void freezeUserByUserID(Integer userID) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getUserID,userID);
        updateWrapper.set(User::getIsFrozen,1);
        userMapper.update(updateWrapper);
    }

    @Override
    public PageBean<UserListVo> getUserList(Long pageNo, Long pageSize) {
        // 创建分页对象
        Page<User> userPage = new Page<>(pageNo, pageSize);

        // 执行查询，返回分页结果
        IPage<User> userIPage = userMapper.selectPage(userPage, null);

        List<User> records = userIPage.getRecords();
        List<UserListVo> userListVos = BeanCopyUtils.copyBeanList(records, UserListVo.class);

        PageBean<UserListVo> result = new PageBean<>();
        result.setPage(pageNo);
        result.setTotal(userIPage.getPages());
        result.setItems(userListVos);
        return result;
    }
}